125,856 Casos confirmados Covid-19

16,550 (13.1%) Fallecimientos


Casos acumulados por tipo (UK)

Comparison

======================================================================= Column {data-width=400}


Nuevos casos diarios confirmados

Predicciones fallecimientos + Observados para UK

Escala Mundial (* + y - iconos para zoom in/out*)

---
title: "Covid-19 United Kingdom"
author: "Cameron Tough y Diego Laorden"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    source_code: embed
    vertical_layout: fill
---
```{r setup, include=FALSE}

#------------------ Packages ------------------

library(flexdashboard)

# install.packages("devtools")

# devtools::install_github("RamiKrispin/coronavirus", force = TRUE)

library(coronavirus)

data(coronavirus)

update_datasets()

# View(coronavirus)

# max(coronavirus$date)



`%>%` <- magrittr::`%>%`

#------------------ Parameters ------------------

# Set colors

# https://www.w3.org/TR/css-color-3/#svg-color

confirmed_color <- "purple"

active_color <- "blue"

recovered_color <- "forestgreen"

death_color <- "red"

#------------------ Data ------------------

df <- coronavirus %>%
  # dplyr::filter(date == max(date)) %>%
  dplyr::filter(Country.Region == "United Kingdom") %>%
  dplyr::group_by(Country.Region, type) %>%
  dplyr::summarise(total = sum(cases)) %>%
  tidyr::pivot_wider(
    names_from = type,
    values_from = total
  ) %>%
  # dplyr::mutate(unrecovered = confirmed - ifelse(is.na(recovered), 0, recovered) - ifelse(is.na(death), 0, death)) %>%
  dplyr::mutate(unrecovered = confirmed - ifelse(is.na(death), 0, death)) %>%
  dplyr::arrange(-confirmed) %>%
  dplyr::ungroup() %>%
  dplyr::mutate(country = dplyr::if_else(Country.Region == "United Arab Emirates", "UAE", Country.Region)) %>%
  dplyr::mutate(country = dplyr::if_else(country == "Mainland China", "China", country)) %>%
  dplyr::mutate(country = dplyr::if_else(country == "North Macedonia", "N.Macedonia", country)) %>%
  dplyr::mutate(country = trimws(country)) %>%
  dplyr::mutate(country = factor(country, levels = country))

df_daily <- coronavirus %>%
  dplyr::filter(Country.Region == "United Kingdom") %>%
  dplyr::group_by(date, type) %>%
  dplyr::summarise(total = sum(cases, na.rm = TRUE)) %>%
  tidyr::pivot_wider(
    names_from = type,
    values_from = total
  ) %>%
  dplyr::arrange(date) %>%
  dplyr::ungroup() %>%
  #dplyr::mutate(active = confirmed - death - recovered) %>%
  dplyr::mutate(active = confirmed - death) %>%
  dplyr::mutate(
    confirmed_cum = cumsum(confirmed),
    death_cum = cumsum(death),
    # recovered_cum = cumsum(recovered),
    active_cum = cumsum(active)
  )


df1 <- coronavirus %>% dplyr::filter(date == max(date))

###########################################################################
#Z. Serie temporal
##########################################
library(readr)
DeathsUK <- read.table("~/DeathsUK.csv", quote="\"", comment.char="")
datosuk<-as.ts(log(DeathsUK))

#############(2,2,3)
ajusteuk<-arima(datosuk,order=c(2,2,3))
lnpredicuk<-predict(ajusteuk,n.ahead = 7)
#Parámetros
ajusteuk$coef
ajusteuk$aic
ajusteuk$loglik
#Predecimos el modelo
predic_AR_UK<-exp(lnpredicuk[[1]])
#Graficamos el modelo predecido
plot(exp(datosuk[]),xlim=c(0,40),ylim=c(0,20000))
lines(predic_AR_UK,col="firebrick")
#################
#Calculamos cuando ocurre el máximo
max(predic_AR_UK)
predic_AR_UK==max(predic_AR_UK) #en 18 días!
##################
#Unimos las dos series: la de valores observados y la de estimados
serie<-c(exp(datosuk),predic_AR_UK)
serie<-as.ts(serie)
plot.ts(serie,col="firebrick")
#El máximo se encuentra en el primer día!!
```

```{r}
valueBox(
  value = paste(format(sum(df$confirmed), big.mark = ","), "", sep = " "),
  caption = "Total confirmed cases",
  icon = "fas fa-user-md",
  color = confirmed_color
)
```
Casos confirmados Covid-19

```{r}

valueBox(
  value = paste(format(sum(df$death, na.rm = TRUE), big.mark = ","), " (",
    round(100 * sum(df$death, na.rm = TRUE) / sum(df$confirmed), 1),
    "%)",
    sep = ""
  ),
  caption = "Death cases (death rate)",
  icon = "fas fa-heart-broken",
  color = death_color
)
```
Fallecimientos

-----------------------------------------------------------------------
### **Casos acumulados por tipo** (UK)


```{r}
plotly::plot_ly(data = df_daily) %>%
  plotly::add_trace(
    x = ~date,
    # y = ~active_cum,
    y = ~confirmed_cum,
    type = "scatter",
    mode = "lines+markers",
    # name = "Active",
    name = "Confirmados",
    line = list(color = active_color),
    marker = list(color = active_color)
  ) %>%
  plotly::add_trace(
    x = ~date,
    y = ~death_cum,
    type = "scatter",
    mode = "lines+markers",
    name = "Fallecimientos",
    line = list(color = death_color),
    marker = list(color = death_color)
  ) %>%
  plotly::add_annotations(
    x = as.Date("2020-01-31"),
    y = 1,
    text = paste("Primer caso"),
    xref = "x",
    yref = "y",
    arrowhead = 5,
    arrowhead = 3,
    arrowsize = 1,
    showarrow = TRUE,
    ax = -10,
    ay = -60
  ) %>%
  plotly::add_annotations(
    x = as.Date("2020-03-05"),
    y = 3,
    text = paste("Primer fallecimiento"),
    xref = "x",
    yref = "y",
    arrowhead = 5,
    arrowhead = 3,
    arrowsize = 1,
    showarrow = TRUE,
    ax = 0,
    ay = -140
  ) %>%
  plotly::add_annotations(
    x = as.Date("2020-03-18"),
    y = 14,
    text = paste(
      "Implementación",
      "
", "Medidas confinamiento" ), xref = "x", yref = "y", arrowhead = 5, arrowhead = 3, arrowsize = 1, showarrow = TRUE, ax = -10, ay = -90 ) %>% plotly::layout( title = "", yaxis = list(title = "Casos Acumulados"), xaxis = list(title = "Date"), legend = list(x = 0.1, y = 0.9), hovermode = "compare" ) ``` Comparison ======================================================================= Column {data-width=400} ------------------------------------- ### **Nuevos casos diarios confirmados** ```{r} daily_confirmed <- coronavirus %>% dplyr::filter(type == "confirmed") %>% dplyr::filter(date >= "2020-02-29") %>% dplyr::mutate(country = Country.Region) %>% dplyr::group_by(date, country) %>% dplyr::summarise(total = sum(cases)) %>% dplyr::ungroup() %>% tidyr::pivot_wider(names_from = country, values_from = total) #---------------------------------------- # Plotting the data daily_confirmed %>% plotly::plot_ly() %>% plotly::add_trace( x = ~date, y = ~`United Kingdom`, type = "scatter", mode = "lines+markers", name = "United Kingdom" ) %>% plotly::add_trace( x = ~date, y = ~Spain, type = "scatter", mode = "lines+markers", name = "Spain" ) %>% plotly::add_trace( x = ~date, y = ~Italy, type = "scatter", mode = "lines+markers", name = "Italy" ) %>% plotly::layout( title = "", legend = list(x = 0.1, y = 0.9), yaxis = list(title = "Number of new confirmed cases"), xaxis = list(title = "Date"), # paper_bgcolor = "black", # plot_bgcolor = "black", # font = list(color = 'white'), hovermode = "compare", margin = list( # l = 60, # r = 40, b = 10, t = 10, pad = 2 ) ) ``` ### **Predicciones fallecimientos + Observados para UK** ```{r} library(plotly) month <- c('8 Abril', '9 Abril', '10 Abril', '11 Abril', '12 Abril', '13 Abril', '14 Abril') Fallecimientos <- c(6666, 7434, 7907, 8620, 9035, 9665, 10010) DatosReales <- c(6171, 7111, 7993, 8974, 9892, 10629, 11347) data <- data.frame(month, Fallecimientos) #The default order will be alphabetized unless specified as below: data$month <- factor(data$month, levels = data[["month"]]) fig <- plot_ly(data, x = ~month, y = ~Fallecimientos, name = 'Fallecimientos', type = 'scatter', mode = 'lines', line = list(color = 'rgb(205, 12, 24)', width = 4)) fig <- fig %>% add_trace(y = ~DatosReales, name = 'Fallecimientos reales', line = list(color = 'rgb(22, 96, 167)', width = 4)) fig <- fig %>% layout(xaxis = list(title = "Fallecidos acumulados"), yaxis = list (title = "Número fallecidos")) fig ``` ### **Escala Mundial** (* + y - iconos para zoom in/out*) ```{r} # map tab added by Art Steinmetz library(leaflet) library(leafpop) library(purrr) cv_data_for_plot <- coronavirus %>% # dplyr::filter(Country.Region == "United Kingdom") %>% dplyr::filter(cases > 0) %>% dplyr::group_by(Country.Region, Province.State, Lat, Long, type) %>% dplyr::summarise(cases = sum(cases)) %>% dplyr::mutate(log_cases = 2 * log(cases)) %>% dplyr::ungroup() cv_data_for_plot.split <- cv_data_for_plot %>% split(cv_data_for_plot$type) pal <- colorFactor(c("orange", "red", "green"), domain = c("confirmed", "death", "recovered")) map_object <- leaflet() %>% addProviderTiles(providers$Stamen.Toner) names(cv_data_for_plot.split) %>% purrr::walk(function(df) { map_object <<- map_object %>% addCircleMarkers( data = cv_data_for_plot.split[[df]], lng = ~Long, lat = ~Lat, # label=~as.character(cases), color = ~ pal(type), stroke = FALSE, fillOpacity = 0.8, radius = ~log_cases, popup = leafpop::popupTable(cv_data_for_plot.split[[df]], feature.id = FALSE, row.numbers = FALSE, zcol = c("type", "cases", "Country.Region", "Province.State") ), group = df, # clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F), labelOptions = labelOptions( noHide = F, direction = "auto" ) ) }) map_object %>% addLayersControl( overlayGroups = names(cv_data_for_plot.split), options = layersControlOptions(collapsed = FALSE) ) ```